﻿using MediatR;
using S.Authorization.Common;
using S.Authorization.Common.Dto;
using S.Authorization.Domain.Model.Authority;
using S.Authorization.Infrastructure.Interface.Menu;
using S.Authorization.Infrastructure.Interface.RoleMenu;
using S.Authorization.Read.API.Dto;
using static S.Authorization.Read.API.Application.Command.MenuCommand.MenuReadAllCommand;

namespace S.Authorization.Read.API.Application.CommandHandlers.MenuCommandHandler
{
    public class MenuReadAllCommandHandler : IRequestHandler<MenuGetByUIdCommand, List<MenuItemDto>>, IRequestHandler<MenuAndOperationGetCommand, List<MenuItemDto>>, IRequestHandler<FeatureMenuRoleGetCommand, Pager<FeatureMenuRoleDto>>, IRequestHandler<MenuGetAllCommand, List<GetMenuListDto>>, IRequestHandler<MenuGetPIdByMIdCommand, List<int>>, IRequestHandler<MenuGetByPIdCommand, List<int>>
    {
        #region 注入
        private readonly IRoleMenuRepository repository;
        private readonly IMenuRepository menuRepository;

        public MenuReadAllCommandHandler(IRoleMenuRepository repository,IMenuRepository menuRepository)
        {
            this.repository = repository;
            this.menuRepository = menuRepository;
        }

        #endregion


        /// <summary>
        /// 显示当前用户下的菜单
        /// </summary>
        /// <returns>返回该用户下的权限菜单</returns>
        public async Task<List<MenuItemDto>> Handle(MenuGetByUIdCommand request, CancellationToken cancellationToken)
        {
            return repository.GetMenuList(request.PId,request.UserId);
        }



        /// <summary>
        /// 获取每个菜单下的所有功能
        /// </summary>
        /// <returns></returns>
        public async Task<List<MenuItemDto>> Handle(MenuAndOperationGetCommand request, CancellationToken cancellationToken)
        {
            return await repository.GetMenuAndOperation(request.MenuId);
        }

        /// <summary>
        /// 显示菜单功能角色三表
        /// </summary>
        /// <returns></returns>
        public async Task<Pager<FeatureMenuRoleDto>> Handle(FeatureMenuRoleGetCommand request, CancellationToken cancellationToken)
        {
            List<FeatureMenuRoleDto> list =await repository.GetFeatureMenuRole(request.MenuId); 
            int totalCount=list.Count;
            int pageCount = (int)Math.Ceiling((totalCount * 1.0) / request.PageSize);
            list=list.Skip((request.PageIndex-1)*request.PageSize).Take(request.PageSize).ToList();
            return new Pager<FeatureMenuRoleDto>()
            {
                TotalCount = totalCount,
                PageCount = pageCount,
                resultData = list
            };
        }

        /// <summary>
        /// 获取菜单表的所有数据
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<List<GetMenuListDto>> Handle(MenuGetAllCommand request, CancellationToken cancellationToken)
        {
            List<MenuList> menuList= await menuRepository.GetAllAsync();
            List<GetMenuListDto> list = new List<GetMenuListDto>();
            menuList.ForEach(x =>
             {
                 GetMenuListDto dto = new GetMenuListDto()
                 {
                     MenuId = x.MenuId,
                     MenuName = x.MenuName
                 };
                 list.Add(dto);
             });
            return list;
           
        }
        /// <summary>
        /// 菜单表--根据MId获取PId
        /// </summary>
        public async Task<List<int>> Handle(MenuGetPIdByMIdCommand request, CancellationToken cancellationToken)
        {
            return await repository.GetMenuPIdByMId(request.MIds);
        }

        public async Task<List<int>> Handle(MenuGetByPIdCommand request, CancellationToken cancellationToken)
        {
            return repository.GetMenuIdByPId(request.PId,request.RoleId);
        }
    }
}
